﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;

namespace PedesTrainProject.GUI
{
    public partial class Cross_Light_Setting : Form
    {
        private SimulationCreator simulationCreator;
        private bool horizontal = true;

        public Cross_Light_Setting(SimulationCreator _simulationCreator)
        {
            InitializeComponent();
            simulationCreator = _simulationCreator;
            
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "hor")
            {   
                groupBox6.Enabled = true;
                groupBox5.Enabled = false;
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "ver")
            {
                groupBox6.Enabled = false;
                groupBox5.Enabled = true;
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "both")
            {
                groupBox6.Enabled = true;
                groupBox5.Enabled = true;
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "none")
            {
                tabPage2.Dispose();
            }
            if (simulationCreator.CoreGUI.getRoadName(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "crossing" || simulationCreator.CoreGUI.getRoadName(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "Tcrossing")
            {
                List<int> intervals = new List<int>();
                intervals = simulationCreator.CoreGUI.getIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY));
             //   comboBox1.Text = "" + intervals[0];
                greenLightNorthBox.Value = intervals[0]/1000;
                redLightNorthBox.Value = intervals[1] / 1000;
                greenLightWestBox.Value = intervals[2] / 1000;
                redLightWestBox.Value = intervals[3] / 1000;
                yellowLightBox.Value = intervals[8] / 1000;
            }
            if (simulationCreator.CoreGUI.getRoadName(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "straight" || simulationCreator.CoreGUI.getRoadName(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "corner")
            {
                tabPage1.Dispose();
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "both")
            {
                textBox1.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[0];
                textBox2.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[1];
                textBox4.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[2];
                textBox3.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[3];
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "hor")
            {
                textBox1.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[0];
                textBox2.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[1];
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "ver")
            {
                textBox4.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[0];
                textBox3.Text = "" + simulationCreator.CoreGUI.getPedestrianIntervals(new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY))[1];
            }  
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            List<int> intervals = new List<int>();
            if (simulationCreator.CoreGUI.getRoadName(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "crossing")
            {
              
                intervals.Add(Convert.ToInt32(greenLightNorthBox.Value*1000));
                intervals.Add(Convert.ToInt32(redLightNorthBox.Value * 1000));
                intervals.Add(Convert.ToInt32(greenLightWestBox.Value * 1000));
                intervals.Add(Convert.ToInt32(redLightWestBox.Value * 1000));
                //intervals.Add(Convert.ToInt32(comboBox1.Text));
                intervals.Add(Convert.ToInt32(greenLightNorthBox.Value * 1000));
                intervals.Add(Convert.ToInt32(redLightNorthBox.Value * 1000));
                intervals.Add(Convert.ToInt32(greenLightWestBox.Value * 1000));
                intervals.Add(Convert.ToInt32(redLightWestBox.Value * 1000));
                intervals.Add(Convert.ToInt32(yellowLightBox.Value * 1000));
                simulationCreator.CoreGUI.setIntervals(intervals, (new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)));
                simulationCreator.CoreGUI.setInitialLights(horizontal, new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY));
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "both")
            {
                intervals.Add(Convert.ToInt32(textBox1.Text));
                intervals.Add(Convert.ToInt32(textBox2.Text));
                intervals.Add(Convert.ToInt32(textBox4.Text));
                intervals.Add(Convert.ToInt32(textBox3.Text));
              
                simulationCreator.CoreGUI.setIntervals(intervals, (new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)));
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "both")
            {
                intervals.Add(Convert.ToInt32(textBox1.Text));
                intervals.Add(Convert.ToInt32(textBox2.Text));
           
                simulationCreator.CoreGUI.setIntervals(intervals, (new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)));
            }
            if (simulationCreator.CoreGUI.getRoadSkin(new System.Windows.Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)) == "both")
            {
                intervals.Add(Convert.ToInt32(textBox4.Text));
                intervals.Add(Convert.ToInt32(textBox3.Text));
               
                simulationCreator.CoreGUI.setIntervals(intervals, (new Vector(simulationCreator.MousetraceX, simulationCreator.MousetraceY)));
            }
          
            
            simulationCreator.Enabled = true;
            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {

        }

        private void Cross_Light_Setting_FormClosed(object sender, FormClosedEventArgs e)
        {
            simulationCreator.Enabled = true;
        }

        private void Cross_Light_Setting_Load(object sender, EventArgs e)
        {
           
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((string)comboBox1.SelectedValue == "West-East")
            {
                horizontal = true;
            }
            if ((string)comboBox1.SelectedValue == "North-South")
            {
                horizontal = false;
            }
        }

        private void redLightNorthBox_ValueChanged(object sender, EventArgs e)
        {
            greenLightWestBox.Value = redLightNorthBox.Value;
        }

        private void greenLightNorthBox_ValueChanged(object sender, EventArgs e)
        {
            redLightWestBox.Value = greenLightNorthBox.Value; 
        }

        private void redLightWestBox_ValueChanged(object sender, EventArgs e)
        {
            greenLightNorthBox.Value = redLightWestBox.Value;
        }

        private void greenLightWestBox_ValueChanged(object sender, EventArgs e)
        {
            redLightNorthBox.Value = greenLightWestBox.Value;
        }
    }
}
